//
//  UserInfoEditViewController.m
//  Romens.XSBalance
//
//  Created by 孙博 on 16/2/23.
//  Copyright © 2016年 孙博. All rights reserved.
//

#import "UserInfoEditViewController.h"
#import "DeviceSearchController.h"
#import "ToolHelper.h"
#import "UserInfoTableViewCell.h"
#import "ZVScrollSlider.h"
#import "userInformation.h"
#import "MyViewController.h"
#import "TranslucentViewController.h"
#import "RuntimeStatus.h"
#import "MJExtension.h"
#import "WebRequest.h"
#import "WebRequestData.h"
#import "PhPTable.h"
#import "GetCurrentTime.h"
#import "UserInfoEditViewController+Save.h"
@interface UserInfoEditViewController ()
{
    TranslucentViewController* tVC;//模态呈现的ViewController
    UserInfoTableViewCell* userInfoCell;
    UIDatePicker *datePicker;//显示生日的日期控件
    
    NSArray* lifeStatus;//存放生活状态内容的数组
    NSString * lifeValue;//存放生活状态的值
    NSArray * lifeImages;//存放生活状态图片的数组
    UIImage * lifeimg;//存放生活状态的图片
    NSArray * lifeNumbers;
    NSString * lifenum;
    NSArray* sexNames;//存放性别内容的数组
    NSArray* sexImages;//存放性别图片的数组
    NSString * sexValue;//存放性别的值
    UIImage * seximg;//存放性别的图片
    UIButton * deleteBt;
    NSString* birthdayValue;//存放生日的值
    NSString* heightValue;//存放身高的值
    NSString* weightValue;//存放体重的值
    NSString* heightUnitValue;//存放身高unit的值
    NSString* weightUnitValue;//存放体重unit的值
    
    NSArray* relationshipArray;//存放成员关系内容的数组
    NSString*relationshipValue;//存放 成员关系的值
    NSArray* relationshipImages;
    UIImage * relationshipimg;
    NSArray * relationshipNumbers;
    NSString * relationshipenum;
    
    NSArray * heightUnits;
    NSArray * weightUnits;
    // PhPTable 类型用于接收服务器传来的 家庭成员信息;
    PhPTable*familyMemberInfo;
}
@end

@implementation UserInfoEditViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.IsShowUserInfo=NO;
    
    // Do any additional setup after loading the view.
    [self initView];
    //隐藏键盘
    [self setupForDismissKeyboard];
    familyMemberInfo=[PhPTable new];
    RuntimeStatus * runtime=[RuntimeStatus instance];
    if (runtime.ALLUsers.count==0) {
        self.title=NSLocalizedString(@"本人资料",nil);
    }
    else
    {
        self.title=NSLocalizedString(@"填写资料",nil);
    }
}
-(void)initView{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"保存",nil)style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    self.detailedTableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.detailedTableView.translatesAutoresizingMaskIntoConstraints=NO;
    self.detailedTableView.dataSource=self;
    self.detailedTableView.delegate=self;
    [self.view addSubview:self.detailedTableView];
    
    deleteBt=[[UIButton alloc]init];
    deleteBt.translatesAutoresizingMaskIntoConstraints=NO;
    [deleteBt setTitle:NSLocalizedString(@"删除",nil) forState:UIControlStateNormal];
    deleteBt.backgroundColor=SystemColor;
    [deleteBt addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deleteBt];
    
    // 创建并初始化NSArray对象
    lifeStatus = [NSArray arrayWithObjects:NSLocalizedString(@"基本无运动",nil),
                  NSLocalizedString(@"办公室人士",nil), NSLocalizedString(@"常站立",nil) , NSLocalizedString(@"从事体力劳动",nil),NSLocalizedString(@"运动员",nil)  ,nil];
    lifeNumbers=[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4", nil];
    lifeImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"nomotion"],[UIImage imageNamed:@"office"],[UIImage imageNamed:@"stand"],[UIImage imageNamed:@"labour"],[UIImage imageNamed:@"athlete"], nil];
    sexNames = [NSArray arrayWithObjects:NSLocalizedString(@"男",nil),NSLocalizedString(@"女",nil), nil];
    sexImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"man"],
                 [UIImage imageNamed:@"woman"], nil];
    relationshipArray=[NSArray arrayWithObjects:NSLocalizedString(@"本人",nil),NSLocalizedString(@"父母",nil),NSLocalizedString(@"子女",nil),NSLocalizedString(@"朋友",nil),NSLocalizedString(@"其他",nil), nil];
    relationshipImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"father"],[UIImage imageNamed:@"mother"],[UIImage imageNamed:@"son"],[UIImage imageNamed:@"mebers"],[UIImage imageNamed:@"elseone"], nil];
    relationshipNumbers=[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4", nil];
    heightUnits = [NSArray arrayWithObjects:@"CM",@"FT", nil];
    weightUnits = [NSArray arrayWithObjects:@"KG",@"LB", nil];
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_detailedTableView) format:@"H:|[_detailedTableView]|"]];
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(deleteBt) format:@"H:|[deleteBt]|"]];
    //接收通知，实现方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(heightNotification:) name:@"height" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weightNotification:) name:@"weight" object:nil];
    
    if (self.userinfor==nil) {
        self.userinfor=[[userInformation alloc]init];
        //新增的时候判断是否是本人；
        RuntimeStatus * run=[RuntimeStatus instance];
        if (run.ALLUsers.count==0) {
            self.userinfor.ISONESELF=@"0";
        }
        else
        {
            self.userinfor.ISONESELF=@"1";
        }
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString *dateStr = [formatter stringFromDate:[NSDate date]];
        self.userinfor.CREATED=dateStr;
        [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_detailedTableView) format:@"V:|[_detailedTableView]|"]];
        deleteBt.hidden=YES;
    }
    else{
        
        [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_detailedTableView) format:@"V:|[_detailedTableView]-40-|"]];
        [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(deleteBt) format:@"V:[deleteBt(40)]|"]];
        deleteBt.hidden=NO;
    }
}
# pragma mark -删除用户信息；
//点击删除按钮响应事件
-(void)delete{
    if ([self.userinfor.ISONESELF isEqualToString:@"0"]) {
        [self SHowMessage:@"本人信息，不允许删除!" type:showWarning];
        return;
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示信息：",nil)message:NSLocalizedString(@"是否删除？",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"否",nil) otherButtonTitles:NSLocalizedString(@"是",nil), nil];
    [alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            break;
        case 1:
        {
            [self DeleteUserInfo];
        }
            break;
    }
}
//身高slider值改变时响应通知方法
- (void)heightNotification:(NSNotification*)notify{
    heightValue=[notify object];
}
//标准体重slider值改变时响应通知方法
- (void)weightNotification:(NSNotification*)notify{
    weightValue=[notify object];
}
//移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  保存成功后会调用的方法
 */
-(void)SaveResultForExec
{
    [self showHint:@"用户信息保存成功"];
    [RuntimeStatus instance].CurrUser=[[RuntimeStatus instance].ALLUsers objectAtIndex:0];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserChange" object:nil];
    if([self.ISONESELF isEqualToString:@"0"]&&[self.IsEdit isEqualToString:@"0"])
    {
        DeviceSearchController*mainview=[[DeviceSearchController alloc] init];
        mainview.IsJump=@"aaaaaa";
        [self.navigationController pushViewController:mainview animated:YES];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//保存按钮响应方法
-(void)save{
    self.userinfor.ISONESELF=self.ISONESELF;
    if (self.userinfor.USERNAME==nil || [self.userinfor.USERNAME isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"昵称不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.SEX==nil||[self.userinfor.SEX isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"性别不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.Birthday==nil||[self.userinfor.Birthday isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"生日不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.Height==nil||[self.userinfor.Height isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"身高不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.HEIGHT_UNIT==nil || [self.userinfor.HEIGHT_UNIT isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"身高单位不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.WEIGHT_UNIT==nil || [self.userinfor.WEIGHT_UNIT isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"体重单位不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.TarghtWeight==nil||[self.userinfor.TarghtWeight isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"体重不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.LifeState==nil||[self.userinfor.LifeState isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"生活状态不能为空.",nil) type:showWarning];
        return;
    }
    if (self.userinfor.RELATIONSHIP==nil||[self.userinfor.RELATIONSHIP isEqualToString:@""])
    {
        [self SHowMessage:NSLocalizedString(@"成员关系不能为空.",nil) type:showWarning];
        return;
    }
    for (userInformation  * people in [RuntimeStatus instance].ALLUsers ) {
        if ([people.ISONESELF isEqualToString:@"0"]&&[self.userinfor.RELATIONSHIP isEqualToString:@"0"]&&deleteBt.hidden) {
            [self SHowMessage:NSLocalizedString(@"已经存在本人，请修改成员关系",nil) type:showWarning];
            return;
        }
    }
    [self SaveData];
}
#pragma mark -tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    userInfoCell=[tableView dequeueReusableCellWithIdentifier:@"userInfoCell"];
    if (userInfoCell==nil) {
        //使用cellID 创建复用机制
        userInfoCell=[[UserInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userInfoCell"];
        userInfoCell.detailedTextField.delegate=self;
    }
    //调用获取cell数据源方法
    [self SetTextCell:userInfoCell path:indexPath];
    return userInfoCell;
}
//封装获取cell数据源的方法
-(void)SetTextCell:(UserInfoTableViewCell*) cell path:(NSIndexPath*) indexPath
{
    cell.detailedTextField.delegate=self;
    cell.detailedTextField.userInteractionEnabled=NO;
    switch (indexPath.row) {
        case 0:
            cell.headerImg.image=[UIImage imageNamed:@"nickname"];
            if (self.userinfor.USERNAME == nil||[self.userinfor.USERNAME isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"昵称",nil);
                cell.detailedTextField.userInteractionEnabled=YES;
                cell.detailedTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
                cell.detailedTextField.returnKeyType =UIReturnKeyDone;
            }else{
                if (deleteBt.hidden) {
                     cell.detailedTextField.userInteractionEnabled=YES;
                }
                cell.detailedTextField.text=self.userinfor.USERNAME;
                if (!deleteBt.hidden) {
                    cell.userInteractionEnabled=NO;
                }
            }
            break;
        case 1:
        {
            if (self.userinfor.SEX == nil||[self.userinfor.SEX isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"性别",nil);
                cell.headerImg.image=[UIImage imageNamed:@"man"];
            }else{
                if ([self.userinfor.SEX isEqualToString:@"1"]) {
                    cell.detailedTextField.text=NSLocalizedString(@"男",nil);
                    cell.headerImg.image=[UIImage imageNamed:@"man"];
                }else if([self.userinfor.SEX isEqualToString:@"0"]){
                    cell.detailedTextField.text=NSLocalizedString(@"女",nil);
                    cell.headerImg.image=[UIImage imageNamed:@"woman"];
                }
                if (!deleteBt.hidden) {
                    cell.userInteractionEnabled=NO;
                }
            }
        }
            break;
        case 2:
        {
            cell.headerImg.image=[UIImage imageNamed:@"birthday"];
            if (self.userinfor.Birthday == nil||[self.userinfor.Birthday isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"生日",nil);
            }else{
                cell.detailedTextField.text=self.userinfor.Birthday;
                if (!deleteBt.hidden) {
                    cell.userInteractionEnabled=NO;
                }
            }
        }
            break;
        case 3:
        {
            cell.headerImg.image=[UIImage imageNamed:@"height"];
            if (self.userinfor.Height == nil||[self.userinfor.Height isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"身高",nil);
            }else{
                cell.detailedTextField.text=self.userinfor.Height;
            }
        }
            break;
        case 4:
        {
            cell.headerImg.image=[UIImage imageNamed:@"heightunit"];
            if (self.userinfor.HEIGHT_UNIT == nil||[self.userinfor.HEIGHT_UNIT isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"身高单位",nil);
            }else{
                
                cell.detailedTextField.text=self.userinfor.HEIGHT_UNIT ;
            }
        }
            break;
        case 5:
        {
            cell.headerImg.image=[UIImage imageNamed:@"weight"];
            if (self.userinfor.TarghtWeight == nil||[self.userinfor.TarghtWeight isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"目标体重",nil);
            }else{
                cell.detailedTextField.text=self.userinfor.TarghtWeight;
            }
        }
            break;
        case 6:
        {
            cell.headerImg.image=[UIImage imageNamed:@"weightunit"];
            if (self.userinfor.WEIGHT_UNIT == nil||[self.userinfor.WEIGHT_UNIT isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"体重单位",nil);
            }else{
                cell.detailedTextField.text=self.userinfor.WEIGHT_UNIT ;
            }
        }
            break;
        case 7:
        {
            if (self.userinfor.LifeState == nil||[self.userinfor.LifeState isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"生活状态",nil);
                cell.headerImg.image=[UIImage imageNamed:@"life"];
            }else{
                int life=[self.userinfor.LifeState  intValue];
                cell.detailedTextField.text=[lifeStatus objectAtIndex:life];
                cell.headerImg.image=[lifeImages objectAtIndex:life];
                if (!deleteBt.hidden) {
                    cell.userInteractionEnabled=YES;
                }
            }
        }
            break;
        case 8:
        {
            if (self.userinfor.RELATIONSHIP == nil||[self.userinfor.RELATIONSHIP isEqualToString:@""]) {
                cell.detailedTextField.placeholder=NSLocalizedString(@"成员关系",nil);
                cell.headerImg.image=[UIImage imageNamed:@"mebers"];
            }else{
                int relation=[self.userinfor.RELATIONSHIP  intValue];
                cell.detailedTextField.text=[relationshipArray objectAtIndex:relation];
                cell.headerImg.image=[relationshipImages objectAtIndex:relation];
                if (!deleteBt.hidden) {
                    cell.userInteractionEnabled=NO;
                }
            }
        }
            break;
    }
}
//textField代理方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.userinfor.USERNAME=textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//半透明窗体
-(void)translucent{
    tVC=[[TranslucentViewController alloc]init];
    tVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        tVC.providesPresentationContextTransitionStyle = YES;
        tVC.definesPresentationContext = YES;
        tVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:tVC animated:YES completion:nil];
    } else {
        self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self presentViewController:tVC animated:NO completion:nil];
        self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    }
}
//显示性别的pickerview控件
-(void)pickerViewType{
    self.sexPickerView=[[UIPickerView alloc]init];
    self.sexPickerView.translatesAutoresizingMaskIntoConstraints=NO;
    self.sexPickerView.dataSource=self;
    self.sexPickerView.delegate=self;
    [tVC.sexView addSubview:self.sexPickerView];
    self.sexPickerView.backgroundColor=[UIColor whiteColor];
    [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexPickerView) format:@"H:|[_sexPickerView]|"]];
    [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexPickerView) format:@"V:|-44-[_sexPickerView]-|"]];
}
//日期控件
-(void)datePickerType{
    datePicker = [[UIDatePicker alloc]init];
    datePicker.translatesAutoresizingMaskIntoConstraints=NO;
    [tVC.sexView addSubview:datePicker];
    [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(datePicker) format:@"H:|[datePicker]|"]];
    [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(datePicker) format:@"V:|-44-[datePicker]-|"]];
    datePicker.backgroundColor=[UIColor whiteColor];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
}
//日期控件滑动改变触发的事件
-(void)dateChanged:(UIDatePicker *)sender{
    NSDate *selectedDate = sender.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateString = [formatter stringFromDate:selectedDate];
    birthdayValue=dateString;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 1:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"性别",nil);
            [self pickerViewType];
            [tVC.okBt addTarget:self action:@selector(sexDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(sexCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 2:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"生日",nil);
            [self datePickerType];
            [tVC.okBt addTarget:self action:@selector(birthdayDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(birthdayCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 3:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"身高",nil);
            CGFloat sliderHeight = [ZVScrollSlider heightWithBoundingWidth:tVC.view.bounds.size.width Title:NSLocalizedString(@"身高",nil)];
            ZVScrollSlider *heightSlider = [[ZVScrollSlider alloc]initWithFrame:CGRectMake(0, 44, tVC.view.bounds.size.width, sliderHeight-10)
                                                                          Title:NSLocalizedString(@"身高",nil)
                                                                       MinValue:0
                                                                       MaxValue:200
                                                                           Step:1
                                                                           Unit:@""
                                                                   HasDeleteBtn:NO];
            
            heightSlider.userInteractionEnabled=YES;
            tVC.sexView.backgroundColor=[UIColor whiteColor];
            [tVC.sexView addSubview:heightSlider];
            [tVC.okBt addTarget:self action:@selector(heightDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(heightCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 4:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"身高单位",nil);
            [self pickerViewType];
            [tVC.okBt addTarget:self action:@selector(heightUnitDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(heightUnitCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 5:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"目标体重",nil);
            CGFloat sliderHeight = [ZVScrollSlider heightWithBoundingWidth:tVC.view.bounds.size.width Title:NSLocalizedString(@"目标体重",nil)];
            ZVScrollSlider *weightSlider = [[ZVScrollSlider alloc]initWithFrame:CGRectMake(0, 44, tVC.view.bounds.size.width, sliderHeight-10)
                                                                          Title:NSLocalizedString(@"目标体重",nil)
                                                                       MinValue:0
                                                                       MaxValue:220
                                                                           Step:1
                                                                           Unit:@""
                                                                   HasDeleteBtn:NO];
            weightSlider.userInteractionEnabled=YES;
            [tVC.sexView addSubview:weightSlider];
            [tVC.okBt addTarget:self action:@selector(weightDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(weightCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 6:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"体重单位",nil);
            [self pickerViewType];
            [tVC.okBt addTarget:self action:@selector(weightUnitDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(weightUnitCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 7:{
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"生活状态",nil);
            self.pickerView=[[UIPickerView alloc]init];
            self.pickerView.translatesAutoresizingMaskIntoConstraints=NO;
            self.pickerView.dataSource = self;
            self.pickerView.delegate = self;
            [tVC.sexView addSubview:self.pickerView];
            self.pickerView.backgroundColor=[UIColor whiteColor];
            [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_pickerView) format:@"H:|[_pickerView]|"]];
            [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_pickerView) format:@"V:|-44-[_pickerView]-|"]];
            [tVC.okBt addTarget:self action:@selector(lifeDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(lifeCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 8:
        {
            [self translucent];
            tVC.sexLabel.text=NSLocalizedString(@"成员关系",nil);
            self.relationshipPickerView=[[UIPickerView alloc]init];
            self.relationshipPickerView.translatesAutoresizingMaskIntoConstraints=NO;
            self.relationshipPickerView.dataSource = self;
            self.relationshipPickerView.delegate = self;
            [tVC.sexView addSubview:self.relationshipPickerView];
            self.relationshipPickerView.backgroundColor=[UIColor whiteColor];
            [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_relationshipPickerView) format:@"H:|[_relationshipPickerView]|"]];
            [tVC.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_relationshipPickerView) format:@"V:|-44-[_relationshipPickerView]-|"]];
            [tVC.okBt addTarget:self action:@selector(relationshipDone) forControlEvents:UIControlEventTouchUpInside];
            [tVC.backBt addTarget:self action:@selector(relationshipCancel) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
    }
}
-(void)heightUnitDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    if (heightUnitValue==nil||[heightUnitValue isEqualToString:@""]) {
        [userInfoCell.detailedTextField setText:@"CM"];
        self.userinfor.HEIGHT_UNIT=@"CM";
    }else{
        [userInfoCell.detailedTextField setText:heightUnitValue];
        if ([userInfoCell.detailedTextField.text isEqualToString:@"CM"]) {
            self.userinfor.HEIGHT_UNIT=@"CM";
        }else{
            self.userinfor.HEIGHT_UNIT=@"FT";
        }
    }
    heightUnitValue=@"";
}
-(void)heightUnitCancel{
    heightUnitValue=@"";
}
-(void)weightUnitDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];
    if (weightUnitValue==nil||[weightUnitValue isEqualToString:@""]) {
        [userInfoCell.detailedTextField setText:@"KG"];
        self.userinfor.WEIGHT_UNIT=@"KG";
    }else{
        [userInfoCell.detailedTextField setText:weightUnitValue];
        if ([userInfoCell.detailedTextField.text isEqualToString:@"KG"]) {
            self.userinfor.WEIGHT_UNIT=@"KG";
        }else{
            self.userinfor.WEIGHT_UNIT=@"LB";
        }
    }
    weightUnitValue=@"";
}
-(void)weightUnitCancel{
    weightUnitValue=@"";
}
-(void)sexDone
{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (sexValue==nil||[sexValue isEqualToString:@""]) {
        [userInfoCell.detailedTextField setText:NSLocalizedString(@"男",nil)];
        [userInfoCell.headerImg setImage:[UIImage imageNamed:@"man"]];
        self.userinfor.SEX=@"1";
    }else{
        [userInfoCell.detailedTextField setText:sexValue];
        [userInfoCell.headerImg setImage:seximg];
        if ([userInfoCell.detailedTextField.text isEqualToString:NSLocalizedString(@"男",nil)]) {
            self.userinfor.SEX=@"1";
        }else{
            self.userinfor.SEX=@"0";
        }
    }
    sexValue=@"";
}
-(void)sexCancel{
    sexValue=@"";
}
-(void)lifeDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:0]];
    if (lifeValue==nil||[lifeValue isEqualToString:@""]) {
        lifeValue=[lifeStatus objectAtIndex:0];
        [userInfoCell.detailedTextField setText:lifeValue];
        [userInfoCell.headerImg setImage:[UIImage imageNamed:@"nomotion"]];
        self.userinfor.LifeState=@"0";
    }else{
        [userInfoCell.detailedTextField setText:lifeValue];
        [userInfoCell.headerImg setImage:lifeimg];
        self.userinfor.LifeState=lifenum;
    }
    lifeValue=@"";
}
-(void)lifeCancel{
    lifeValue=@"";
}
-(void)relationshipDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:8 inSection:0]];
    if (relationshipValue==nil||[relationshipValue isEqualToString:@""]) {
        relationshipValue=[relationshipArray objectAtIndex:0];
        [userInfoCell.detailedTextField setText:relationshipValue];
        self.userinfor.RELATIONSHIP=@"0";
        [userInfoCell.headerImg setImage:[UIImage imageNamed:@"mebers"]];
    }else{
        [userInfoCell.detailedTextField setText:relationshipValue];
        [userInfoCell.headerImg setImage:relationshipimg];
        self.userinfor.RELATIONSHIP=relationshipenum;
    }
    relationshipValue=@"";
}
-(void)relationshipCancel{
    relationshipValue=@"";
}
-(void)birthdayDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    if (birthdayValue==nil||[birthdayValue isEqualToString:@""]) {
        datePicker.datePickerMode = UIDatePickerModeDate;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString *dateStr = [formatter stringFromDate:[NSDate date]];
        userInfoCell.detailedTextField.text=dateStr;
    }
    else{
        userInfoCell.detailedTextField.text=birthdayValue;
    }
    self.userinfor.Birthday=userInfoCell.detailedTextField.text;
    birthdayValue=@"";
}
-(void)birthdayCancel{
    birthdayValue=@"";
}
-(void)heightDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    
    if (heightValue==nil||[heightValue isEqualToString:@""]) {
        NSString * heightDe=@"160";
        userInfoCell.detailedTextField.text=heightDe;
    }else{
        userInfoCell.detailedTextField.text=heightValue;
    }
    self.userinfor.Height=userInfoCell.detailedTextField.text;
    heightValue=@"";
}
-(void)heightCancel{
    heightValue=@"";
}
-(void)weightDone{
    userInfoCell=[self.detailedTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
    if (weightValue==nil||[weightValue isEqualToString:@""]) {
        NSString * weightDe=@"60";
        userInfoCell.detailedTextField.text=weightDe;
    }else{
        userInfoCell.detailedTextField.text=weightValue;
    }
    self.userinfor.TarghtWeight=userInfoCell.detailedTextField.text;
    weightValue=@"";
}
-(void)weightCancel{
    weightValue=@"";
}
// UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件包含多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;  // 返回1表明该控件只包含1列
}
// UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    // 由于该控件只包含一列，因此无须理会列序号参数component
    // 该方法返回books.count，表明books包含多少个元素，该控件就包含多少行
    if (pickerView==self.pickerView) {
        return lifeStatus.count;
    }
    else if (pickerView==self.sexPickerView){
        return sexNames.count;
    }
    else if (pickerView==self.relationshipPickerView){
        return relationshipArray.count;
    }
    return 0;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (pickerView==self.sexPickerView) {
        UIView * sexView=[[UIView alloc]init];
        UIImageView * imgG=[[UIImageView alloc]init];
        imgG.translatesAutoresizingMaskIntoConstraints=NO;
        imgG.contentMode=UIViewContentModeScaleAspectFit;
        [sexView addSubview:imgG];
        UILabel * sexlabel=[[UILabel alloc]init];
        sexlabel.translatesAutoresizingMaskIntoConstraints=NO;
        if ( [tVC.sexLabel.text isEqualToString:NSLocalizedString(@"性别",nil)]) {
            [imgG setImage:[sexImages objectAtIndex:row]];
            [sexlabel setText:[sexNames objectAtIndex:row]];
        }else if ([tVC.sexLabel.text isEqualToString:NSLocalizedString(@"身高单位",nil)]){
            [sexlabel setText:[heightUnits objectAtIndex:row]];
            
        }else if ([tVC.sexLabel.text isEqualToString:NSLocalizedString(@"体重单位",nil)]){
            [sexlabel setText:[weightUnits objectAtIndex:row]];
        }
        [sexlabel sizeToFit];
        sexlabel.font=systemFont(20);
        [sexView addSubview:sexlabel];
        int w= (FULL_WIDTH-55)/2;
        [sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG,sexlabel) format:[NSString stringWithFormat:@"H:|-(%d)-[imgG(20)][sexlabel(40)]",w]]];
        [sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG) format:@"V:|[imgG]|"]];
        [sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(sexlabel) format:@"V:|[sexlabel]|"]];
        return sexView;
    }else if (pickerView==self.pickerView){
        UIView * lifeView=[[UIView alloc]init];
        UIImageView * imgG=[[UIImageView alloc]init];
        imgG.translatesAutoresizingMaskIntoConstraints=NO;
        imgG.contentMode=UIViewContentModeScaleAspectFit;
        [imgG setImage:[lifeImages objectAtIndex:row]];
        [lifeView addSubview:imgG];
        UILabel * label=[[UILabel alloc]init];
        label.translatesAutoresizingMaskIntoConstraints=NO;
        [label setText:[lifeStatus objectAtIndex:row]];
        [label sizeToFit];
        label.font=systemFont(20);
        [lifeView addSubview:label];
        [lifeView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG,label) format:@"H:|-90-[imgG(25)]-[label]|"]];
        [lifeView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG) format:@"V:|[imgG]|"]];
        [lifeView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(label) format:@"V:|[label]|"]];
        return lifeView;
    }else{
        UIView * relationshipView=[[UIView alloc]init];
        UIImageView * imgG=[[UIImageView alloc]init];
        imgG.translatesAutoresizingMaskIntoConstraints=NO;
        imgG.contentMode=UIViewContentModeScaleAspectFit;
        
        [imgG setImage:[relationshipImages objectAtIndex:row]];
        [relationshipView addSubview:imgG];
        UILabel * label=[[UILabel alloc]init];
        label.translatesAutoresizingMaskIntoConstraints=NO;
        [label setText:[relationshipArray objectAtIndex:row]];
        [label sizeToFit];
        label.font=systemFont(20);
        [relationshipView addSubview:label];
        [relationshipView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG,label) format:@"H:|-90-[imgG(25)]-[label]|"]];
        [relationshipView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(imgG) format:@"V:|[imgG]|"]];
        [relationshipView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(label) format:@"V:|[label]|"]];
        return relationshipView;
    }
}
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView==self.pickerView) {
        lifeValue=[lifeStatus objectAtIndex:row];
        lifeimg=[lifeImages objectAtIndex:row];
        lifenum=[lifeNumbers objectAtIndex:row];
    }else if (pickerView==self.sexPickerView){
        if ( [tVC.sexLabel.text isEqualToString:NSLocalizedString(@"性别",nil)]) {
            sexValue=[sexNames objectAtIndex:row];
            seximg=[sexImages objectAtIndex:row];
        }else if ([tVC.sexLabel.text isEqualToString:NSLocalizedString(@"身高单位",nil)]){
            heightUnitValue=[heightUnits objectAtIndex:row];
        }else if ([tVC.sexLabel.text isEqualToString:NSLocalizedString(@"体重单位",nil)]){
            weightUnitValue=[weightUnits objectAtIndex:row];
        }
    }else if (pickerView==_relationshipPickerView)
    {
        relationshipValue=[relationshipArray objectAtIndex:row];
        relationshipimg=[relationshipImages objectAtIndex:row];
        relationshipenum=[relationshipNumbers objectAtIndex:row];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
